package www.wmx.com.xssc.util.threeUtil;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.wmx.com.xssc.domain.doc.jcyjj.*;
import www.wmx.com.xssc.domain.mb.RbtKjjlMbJcyjjNum;
import www.wmx.com.xssc.domain.vo.extract.BaseMatchResult;
import www.wmx.com.xssc.domain.vo.extract.request.LWRequest;
import www.wmx.com.xssc.domain.vo.extract.request.PatentRequest;
import www.wmx.com.xssc.domain.vo.extract.request.ZZRequest;
import www.wmx.com.xssc.domain.vo.response.JCYJJResponse;
import www.wmx.com.xssc.service.IRbtKjjlMbJcyjjNumService;
import www.wmx.com.xssc.service.jcyjj.*;
import www.wmx.com.xssc.util.ArticleProcessUtil;
import www.wmx.com.xssc.util.PatentProcessUtil;
import www.wmx.com.xssc.util.ZZProcessUtil;
import www.wmx.com.xssc.util.enumUtil.CgjlType;
import www.wmx.com.xssc.util.time.DateFormatterUtil;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础研究奖各个成果奖项的罗列:
 */
@Data
@Component
@Slf4j
public class JcyjjProcessUtil {


    @Resource(description = "基础研究奖-论文-RbtKjjlJcyjjLwxx")
    private IRbtKjjlJcyjjLwxxService jcyjjLwxxService;
    @Resource(description = "基础研究奖-专利-RbtKjjlJcyjjFmzlzm")
    private IRbtKjjlJcyjjFmzlzmService jcyjjFmzlzmService;
    @Resource(description = "基础研究奖-专著-RbtKjjlJcyjjZzxx")
    private IRbtKjjlJcyjjZzxxService jcyjjZzxxService;
    @Resource(description = "基础研究奖的基本信息")
    private IRbtKjjlJcyjjJbxxService jcyjjJbxxService;
    @Resource
    private ArticleProcessUtil articleProcessUtil;
    @Resource
    private PatentProcessUtil patentProcessUtil;
    @Resource
    private ZZProcessUtil zzProcessUtil;
    @Resource(description = "成果登记的信息")
    private IRbtKjjlJcyjjBjcgService jcyjjBjcgService;





    /**
     * 基础研究奖所有成果的处理过程：
     *
     * @param jbxxID
     */
    public JCYJJResponse processJcyjj(String jbxxID) {

        boolean flag = true;
        JCYJJResponse jcyjjResponse = new JCYJJResponse();
        List<LWRequest> lw = new ArrayList<>();
        List<PatentRequest> zl = new ArrayList<>();
        List<ZZRequest> zz = new ArrayList<>();


        RbtKjjlJcyjjJbxx jcyjjJbxx = jcyjjJbxxService.lambdaQuery()
                .eq(RbtKjjlJcyjjJbxx::getId, jbxxID)
                .one();

        //0.成果登记：
        String cgdjFailReson = null;
        List<RbtKjjlJcyjjBjcg> jcyjjBjcgList = jcyjjBjcgService.lambdaQuery()
                .in(RbtKjjlJcyjjBjcg::getCgglDjId, jcyjjJbxx.getCgglDjId().split("，,"))
                .list();
        for (RbtKjjlJcyjjBjcg jcyjjBjcg : jcyjjBjcgList) {
            if (!(jcyjjBjcg != null && jcyjjBjcg.getKjjlJcyjjId() != null && jcyjjBjcg.getKjjlJcyjjId().equals(jcyjjJbxx.getId()))) {
                cgdjFailReson = "项目成果未完成成果登记";
                jcyjjResponse.setCgdjFailReson(cgdjFailReson);
                flag = false;
                break;
            }
        }

        //1.论文：
        List<RbtKjjlJcyjjLwxx> jcyjjLwxxList = jcyjjLwxxService.lambdaQuery()
                .eq(RbtKjjlJcyjjLwxx::getKjjlJcyjjId, jbxxID)
                .list();
        if (jcyjjLwxxList != null && !jcyjjLwxxList.isEmpty()) {
            articleProcessUtil.init();
            for (RbtKjjlJcyjjLwxx lwxx : jcyjjLwxxList) {
                LWRequest lwRequest = initLwRequest(jcyjjJbxx, lwxx);
                BaseMatchResult result = articleProcessUtil.matchProcess(lwRequest);
                if (!result.isSuccess()) {
                    lwRequest.setFailReason(result.getFailReason());
                    lw.add(lwRequest);
                    flag = false;
                }
            }
        }
        //2.专利：
        List<RbtKjjlJcyjjFmzlzm> jcyjjFmzlzmList = jcyjjFmzlzmService.lambdaQuery()
                .eq(RbtKjjlJcyjjFmzlzm::getKjjlJcyjjId, jbxxID)
                .list();
        if (jcyjjFmzlzmList != null && !jcyjjFmzlzmList.isEmpty()) {
            patentProcessUtil.init();
            for (RbtKjjlJcyjjFmzlzm fmzlzm : jcyjjFmzlzmList) {
                PatentRequest patentRequest = initPatentRequest(jcyjjJbxx, fmzlzm);
                BaseMatchResult result = patentProcessUtil.matchProcess(patentRequest);
                if (!result.isSuccess()) {
                    patentRequest.setFailReason(result.getFailReason());
                    zl.add(patentRequest);
                    flag = false;
                }
            }
        }
        //3.专著：
        List<RbtKjjlJcyjjZzxx> jcyjjZzxxList = jcyjjZzxxService.lambdaQuery()
                .eq(RbtKjjlJcyjjZzxx::getKjjlJcyjjId, jbxxID)
                .list();
        if (jcyjjZzxxList != null && !jcyjjZzxxList.isEmpty()) {
            zzProcessUtil.init();
            for (RbtKjjlJcyjjZzxx zzxx : jcyjjZzxxList) {
                ZZRequest zzRequest = initZZRequest(jcyjjJbxx, zzxx);
                BaseMatchResult result = zzProcessUtil.matchProcess(zzRequest);
                if (!result.isSuccess()) {
                    zzRequest.setFailReason(result.getFailReason());
                    zz.add(zzRequest);
                    flag = false;
                }
            }
        }
        jcyjjResponse.setLwRequestList(lw);
        jcyjjResponse.setZlRequestList(zl);
        jcyjjResponse.setZzRequestList(zz);
        jcyjjResponse.setSuccess(flag);
        return jcyjjResponse;

    }

    private LWRequest initLwRequest(RbtKjjlJcyjjJbxx jcyjjJbxx, RbtKjjlJcyjjLwxx lwxx) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        List<String> names = Arrays.stream(lwxx.getDyzz().split("、,，；;")).collect(Collectors.toList());
        LWRequest lwRequest = LWRequest.builder()
                .authorEnroll(names)
                .lwtm(lwxx.getLwtm())
                .qkmc(lwxx.getQkmc())
                .fbny(LocalDate.from(DateFormatterUtil.StrToLocalDateTime(lwxx.getFbny())))
                .qtzz(lwxx.getQtzz())
                .zzpm(lwxx.getQtzz())
                .sciTycs(lwxx.getSciTycs())
                .tycs(lwxx.getTycs())
                .yxyz(lwxx.getYxyz())
                .build();
        lwRequest.setNd(jcyjjJbxx.getNd());
        lwRequest.setXmmc(jcyjjJbxx.getBjmc());
        lwRequest.setJbxxID(jcyjjJbxx.getId());
        lwRequest.setCgjlType(CgjlType.JCYJJ.getCode());
        lwRequest.setXmBeginTime(start);
        lwRequest.setXmEndTime(end);
        return lwRequest;
    }

    private PatentRequest initPatentRequest(RbtKjjlJcyjjJbxx jcyjjJbxx, RbtKjjlJcyjjFmzlzm fmzlzm) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        PatentRequest patentRequest = PatentRequest
                .builder()
                .zlmc(fmzlzm.getZlmc())
                .fmr(fmzlzm.getFmr())
                .fmrList(Arrays.stream(fmzlzm.getFmr().split("、,，；;")).collect(Collectors.toList()))
                .zlh(fmzlzm.getZlh())
                .build();
        patentRequest.setNd(jcyjjJbxx.getNd());
        patentRequest.setXmmc(jcyjjJbxx.getBjmc());
        patentRequest.setJbxxID(jcyjjJbxx.getId());
        patentRequest.setCgjlType(CgjlType.JCYJJ.getCode());
        patentRequest.setXmBeginTime(start);
        patentRequest.setXmEndTime(end);
        return patentRequest;
    }

    private ZZRequest initZZRequest(RbtKjjlJcyjjJbxx jcyjjJbxx, RbtKjjlJcyjjZzxx zzxx) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        ZZRequest zzRequest = ZZRequest
                .builder()
                .zzmc(zzxx.getZzsmc())
                .sbsj(zzxx.getSbsj())
                .fbsj(LocalDate.from(DateFormatterUtil.StrToLocalDateTime(zzxx.getSbsj())))
                .zz(zzxx.getZz())
                .authorEnroll(Arrays.stream(zzxx.getZz().split("、,，；;")).collect(Collectors.toList()))
                .build();
        zzRequest.setNd(jcyjjJbxx.getNd());
        zzRequest.setXmmc(jcyjjJbxx.getBjmc());
        zzRequest.setJbxxID(jcyjjJbxx.getId());
        zzRequest.setCgjlType(CgjlType.JCYJJ.getCode());
        zzRequest.setXmBeginTime(start);
        zzRequest.setXmEndTime(end);
        return zzRequest;
    }


}
